package com.fau.ss.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.ss.action.PublicAction;
import com.fau.ss.entity.MedicalAdmin;
import com.fau.ss.entity.MedicalSsPurchase;
import com.fau.ss.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 购买力统计Action
 * @author liy
 * @datetime 2014年10月30日,下午9:50:29
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class PurchasingPowerStaAction extends PublicAction implements ModelDriven<MedicalSsPurchase>{

	private static final long serialVersionUID = -4743165361566354326L;
	
	private MedicalSsPurchase purchase = new MedicalSsPurchase();

	/**
	 * @desc 进入购买力统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年10月30日,下午9:54:13
	 */
	@Action(value = "purchasing_power_init", results = {@Result(name = SUCCESS, location = "purchasing_power_init.jsp")})
	public String init(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入基本统计页面
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月10日,上午9:44:32
	 */
	@Action(value = "purchasing_power_basic", results = {@Result(name = SUCCESS, location = "purchasing_power_basic.jsp")})
	public String basic() {
		return SUCCESS;
	}
	
	/**
	 * @desc 分页查询购买力基本统计数据
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月10日,下午3:14:11
	 */
	@Action(value = "purchasing_power_basic_view")
	public String basicView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getPurchasingPowerStaService().findPurPowerBasicByPage(purchase, admin, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}

	/**
	 * @desc 进入药品类别统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,下午1:43:56
	 */
	@Action(value = "purchasing_power_medicine", results = {@Result(name = SUCCESS, location = "purchasing_power_medicine.jsp")})
	public String medicine(){
		return SUCCESS;
	}
	
	/**
	 * @desc 加载以药品类别统计数据
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月11日,下午3:18:35
	 */
	@Action(value = "purchasing_power_medicine_view")
	public String medicineView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = ServletActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		pageBean = this.baseAllService.getPurchasingPowerStaService().findPurPowerMedicineByPage(purchase, admin, map);
		map.clear();
		map.put("total", pageBean.getTotalRow());
		map.put("data", pageBean.getResult());
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询药品类别树形集合
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月11日,下午4:44:27
	 */
	@Action(value = "purchasing_power_medicine_sort")
	public String findMedicineSort() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getPurchasingPowerStaService().findMedicineSortList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 按年龄段统计（柱状图）
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月11日,下午6:09:41
	 */
	@Action(value = "purchasing_power_statistics_age")
	public String StatisticsByAgeRange() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getPurchasingPowerStaService().StatisticsByAgeRange(admin, purchase);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 进入年龄段统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,上午11:37:17
	 */
	@Action(value = "purchasing_power_age", results = {@Result(name = SUCCESS, location = "purchasing_power_age.jsp")})
	public String age(){
		return SUCCESS;
	}
	
	/**
	 * @desc 进入年度统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,上午11:44:11
	 */
	@Action(value = "purchasing_power_year", results = {@Result(name = SUCCESS, location = "purchasing_power_year.jsp")})
	public String year(){
		return SUCCESS;
	}
	
	/**
	 * @desc 年度统计数据展示
	 * @return String
	 * @author dangjh
	 * @datetime 2014年12月3日 下午5:47:07
	 */
	@Action(value = "purchasing_power_statistics_year")
	public String StatisticsByYear() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getPurchasingPowerStaService().StatisticsByYear(admin, purchase);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 进入月份统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,下午1:36:25
	 */
	@Action(value = "purchasing_power_month", results = {@Result(name = SUCCESS, location = "purchasing_power_month.jsp")})
	public String month(){
		return SUCCESS;
	}
	
	/**
	 * @desc 以月份统计（柱状图）
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月13日,上午12:08:58
	 */
	@Action(value = "purchasing_power_statistics_month")
	public String StatisticsByMonth() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getPurchasingPowerStaService().StatisticsByMonth(admin, purchase);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 进入会员等级统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,下午2:10:20
	 */
	@Action(value = "purchasing_power_level", results = {@Result(name = SUCCESS, location = "purchasing_power_level.jsp")})
	public String level(){
		return SUCCESS;
	}
	
	/**
	 * @desc 以会员等级统计（柱状图）
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月13日,上午10:19:24
	 */
	@Action(value = "purchasing_power_statistics_level")
	public String StatisticsByLevel() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = this.baseAllService.getPurchasingPowerStaService().StatisticsByLevel(admin, purchase);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 进入宣传区域统计页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月1日,下午2:32:06
	 */
	@Action(value = "purchasing_power_area", results = {@Result(name = SUCCESS, location = "purchasing_power_area.jsp")})
	public String area(){
		return SUCCESS;
	}
	
	/**
	 * @desc 按地区统计
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月19日,下午9:12:19
	 */
	@Action(value = "purchasing_power_statistics_area")
	public String StatisticsByArea() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		map = this.baseAllService.getPurchasingPowerStaService().StatisticsByCity(admin, purchase);
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询当前用户所在企业所有中心
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月19日,上午2:17:09
	 */
	@Action(value = "purchasing_power_find_center")
	public String findCenterList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getPurchasingPowerStaService().findCenterList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询中心下组的集合
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月19日,上午8:59:59
	 */
	@Action(value = "purchasing_power_find_group")
	public String findGroupList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getPurchasingPowerStaService().findGroupListByCid(purchase.getCenterId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 查询组下话务员集合
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月19日,上午9:01:39
	 */
	@Action(value = "purchasing_power_find_user")
	public String findUsersList() {
		HttpServletResponse response = ServletActionContext.getResponse();
		list = this.baseAllService.getPurchasingPowerStaService().findUserListByGid(purchase.getGroupId());
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 将客户分配到话务员
	 * @return String
	 * @author kangyj
	 * @datetime 2014年11月20日,上午10:15:54
	 */
	@Action(value = "purchasing_power_allocate")
	public String allocate() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(purchase.getPhoneNum()) && StringUtils.isNotBlank(purchase.getUserId())) {
			this.baseAllService.getPurchasingPowerStaService().allocate(purchase.getPhoneNum(), purchase.getUserId());
			map.put("success", true);
		}else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 购买力基本统计导出
	 * @return null
	 * @author liy
	 * @datetime 2014年12月30日,下午8:55:55
	 */
	@Action(value = "purchasing_power_export_all")
	public String allExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"购买力基本统计导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getPurchasingPowerStaService().exportBasicAll(this.purchase);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 购买力药品类别统计导出
	 * @return null
	 * @author liy
	 * @datetime 2014年12月30日,下午9:52:46
	 */
	@Action(value = "purchasing_power_ms_export_all")
	public String medicineSortAllExport() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"购买力药品类别统计导出.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getPurchasingPowerStaService().medicineSortExportBasicAll(this.purchase);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public MedicalSsPurchase getModel() {
		return purchase;
	}
}
